# Copyright 2023 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_python//sphinxdocs:sphinx_docs_library.bzl", "sphinx_docs_library")
load("//pw_build:compatibility.bzl", "incompatible_with_mcu")
load("//pw_build:pw_facade.bzl", "pw_facade")
load("//pw_unit_test:pw_cc_test.bzl", "pw_cc_test")

package(
    default_visibility = ["//visibility:public"],
)

licenses(["notice"])

cc_library(
    name = "dispatcher",
    hdrs = [
        "public/pw_async/dispatcher.h",
        "public/pw_async/function_dispatcher.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":types",
        "//pw_chrono:system_clock",
        "//pw_function",
        "//pw_status",
    ],
)

pw_facade(
    name = "task",
    hdrs = ["public/pw_async/task.h"],
    backend = ":task_backend",
    strip_include_prefix = "public",
    deps = [
        ":types",
        "//pw_chrono:system_clock",
        "//pw_function",
        "//pw_status",
    ],
)

label_flag(
    name = "task_backend",
    build_setting_default = "//pw_async_basic:task",
)

cc_library(
    name = "types",
    hdrs = [
        "public/pw_async/context.h",
        "public/pw_async/task_function.h",
    ],
    strip_include_prefix = "public",
    deps = [
        "//pw_function",
        "//pw_status",
    ],
)

pw_facade(
    name = "fake_dispatcher",
    testonly = True,
    hdrs = ["public/pw_async/fake_dispatcher.h"],
    backend = ":fake_dispatcher_backend",
    strip_include_prefix = "public",
    deps = [":dispatcher"],
)

label_flag(
    name = "fake_dispatcher_backend",
    build_setting_default = "//pw_async_basic:fake_dispatcher",
)

pw_cc_test(
    name = "fake_dispatcher_test",
    srcs = ["fake_dispatcher_test.cc"],
    deps = [
        ":fake_dispatcher",
        "//pw_chrono:system_clock",
        "//pw_containers:vector",
        "//pw_log",
        "//pw_string:to_string",
        "//pw_sync:timed_thread_notification",
        "//pw_thread:thread",
    ],
)

cc_library(
    name = "fake_dispatcher_fixture",
    testonly = True,
    hdrs = ["public/pw_async/fake_dispatcher_fixture.h"],
    strip_include_prefix = "public",
    deps = [
        ":fake_dispatcher",
        "//pw_unit_test",
    ],
)

cc_library(
    name = "heap_dispatcher",
    srcs = ["heap_dispatcher.cc"],
    hdrs = ["public/pw_async/heap_dispatcher.h"],
    strip_include_prefix = "public",
    deps = [
        ":dispatcher",
        ":task",
        ":types",
        "//pw_result",
    ],
)

filegroup(
    name = "doxygen",
    srcs = [
        "public/pw_async/context.h",
        "public/pw_async/dispatcher.h",
        "public/pw_async/fake_dispatcher_fixture.h",
        "public/pw_async/function_dispatcher.h",
        "public/pw_async/heap_dispatcher.h",
        "public/pw_async/task.h",
        "public/pw_async/task_function.h",
    ],
)

sphinx_docs_library(
    name = "docs",
    srcs = [
        "backends.rst",
        "docs.rst",
    ],
    prefix = "pw_async/",
    target_compatible_with = incompatible_with_mcu(),
)
